import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'function.dart';
import 'package:flutter_qq/flutter_qq.dart';
import 'widget/gradient_button.dart';
import 'util/ui_data.dart';
import 'dart:io';

class LoginPage extends StatefulWidget {
  final Color mainColor;

  LoginPage(this.mainColor);

  @override
  _LoginPageState createState() => new _LoginPageState(mainColor);
}

class _LoginPageState extends State<LoginPage> {
  _LoginPageState(this.mainColor);

  final scaffoldState = GlobalKey<ScaffoldState>();
  String account = "";
  String password = "";
  bool showPass = false;
  final Color mainColor;
  var deviceSize;
  bool isReg = false;

  @override
  void initState() {
    super.initState();
  }

  String validateAccount(String value) {
    if (value == "") return "请输入手机号";
    return null;
  }

  String validatePassword(String value) {
    if (value == "") return "请输入密码";
    return null;
  }

  login() async {}

  @override
  Widget build(BuildContext context) {
    deviceSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Color(0xffeeeeee),
      key: scaffoldState,
      appBar: new CupertinoNavigationBar(
        border: new Border(bottom: BorderSide.none),
        actionsForegroundColor: Colors.white,
        backgroundColor: mainColor,
        middle: new Text(
          "登录",
          style: new TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      body: new SingleChildScrollView(
        child: new Column(
          children: <Widget>[
            new Container(
              height: 100.0,
              child: ClipPath(
                clipper: new ArcClipper(),
                child: Stack(
                  children: <Widget>[
                    new Container(
                      decoration: new BoxDecoration(
                        gradient: new LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [mainColor, mainColor],
                        ),
                      ),
                    ),
                    new Container(
                      width: double.infinity,
                      child: Image.asset(
                        "images/icon.png",
                        width: 40.0,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            new Card(
              color: Colors.white,
              margin: EdgeInsets.all(20.0),
              elevation: 2.0,
              child: Form(
                child: Padding(
                  padding: const EdgeInsets.all(18.0),
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      new TextField(
                        onChanged: (phone) => account = phone,
                        style: new TextStyle(fontSize: 15.0, color: Colors.black),
                        decoration: new InputDecoration(
                          hintText: "请输入账号",
                          labelText: "账号",
                          labelStyle: TextStyle(
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                      ),
                      new SizedBox(
                        height: 10.0,
                      ),
                      new TextField(
                        onChanged: (value) {
                          password = value;
                        },
                        style: new TextStyle(
                          fontSize: 15.0,
                          color: Colors.black,
                        ),
                        decoration: new InputDecoration(
                          hintText: "请输入密码",
                          labelText: "密码",
                          labelStyle: TextStyle(
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                        obscureText: true,
                      ),
                      new SizedBox(
                        height: 30.0,
                      ),
                      new GradientButton(
                        onPressed: () async {
                          if (isReg) {
                            var regRes = await userRegister(this.account, this.password);
                            if (regRes['code'] == 0) {
                              toast(regRes['msg']);
                            } else {
                              toast("注册并登录成功");
                              setStorage("uid", regRes['msg']['uid'].toString());
                              Navigator.of(context).pop(regRes['msg']['uid'].toString());
                            }
                          } else {
                            var loginRes = await userLogin(this.account, this.password);
                            if (loginRes['code'] == 0) {
                              toast(loginRes['msg']);
                            } else {
                              toast("登录成功");
                              setStorage("uid", loginRes['msg']['uid'].toString());
                              Navigator.of(context).pop(loginRes['msg']['uid'].toString());
                            }
                          }
                        },
                        text: isReg ? "注册账号" : "立即登录",
                        splashColor: mainColor,
                      ),
                      new SizedBox(
                        height: 10.0,
                      ),
                      new Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          new FlatButton(
                            child: Text("注册账号"),
                            onPressed: () async {
                              isReg = !isReg;
                              setState(() {});
                            },
                          ),
                          new FlatButton(
                            child: Text("QQ登录"),
                            onPressed: () async {
                              if (Platform.isIOS) {
                                FlutterQq.registerQQ("101218757");
                              } else {
                                FlutterQq.registerQQ("101475558");
                              }

                              var qqResult = await FlutterQq.login();
                              print(qqResult.response);
                              if (qqResult.code == 0) {
                                var qqInfo = await getQQInfo(qqResult.response['accessToken'], qqResult.response['openid']);
                                print(qqInfo);
                                var loginRes = await QQLogin(qqResult.response['openid'], qqInfo['nickname'], qqInfo['figureurl_qq_2']);
                                if (loginRes['code'] == 0) {
                                  toast("登录出错，请重试");
                                } else {
                                  toast("登录成功");
                                  setStorage("uid", loginRes['msg']['uid'].toString());
                                  Navigator.of(context).pop(loginRes['msg']['uid'].toString());
                                }
                              } else if (qqResult.code == 1) {
                                print("登录失败" + qqResult.message);
                                toast("登录失败");
                              } else {
                                print("取消登录");
                                toast("取消登录");
                              }
                            },
                          ),
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
